package com.zjp.blog.controller.manage;

import com.zjp.blog.base.ApplicationContext;
import com.zjp.blog.base.globalValName;
import com.zjp.blog.data.model.User;
import com.zjp.blog.utils.MD5;
import com.zjp.blog.controller.BaseController;
import com.zjp.blog.service.UserService;
import com.zjp.blog.utils.Validation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

/**
 * @author pj_zhong
 * @email pj_zhong@163.com / pj_zhong@gmail.com
 */
@Controller
@RequestMapping("/manage")
public class ManageController extends BaseController
{
    private static final Logger logger = LogManager.getLogger(ManageController.class);

    public  static String to_login="/manage/login";
    public  static String page_login="/manage/admin/login";
    public  static String manage_home="/manage/admin/home";

    @Autowired
    UserService service;

    @Autowired
    ApplicationContext appContext;

    @RequestMapping(method = RequestMethod.GET)
    public String home(ModelMap map)
    {
        Runtime jvm = Runtime.getRuntime();
        long MB = 1024L*1024L;

        float freeMemory  = (float) jvm.freeMemory()/MB;
        float totalMemory = (float)jvm.totalMemory()/MB;
        float usedMemory  = (totalMemory - freeMemory);
        int memoryPercent = Math.round ( usedMemory / totalMemory * 100);
        String os  = System.getProperty("os.name");
        String javaVersion = System.getProperty("java.version");

        map.addAttribute("memoryPercent",memoryPercent);
        map.addAttribute("usedMemory",usedMemory);
        map.addAttribute("totalMemory",totalMemory);
        map.addAttribute("os",os);
        map.addAttribute("javaVersion",javaVersion);
        map.addAttribute("configs",appContext.getSystemConfigs());

        return manage_home;
    }

/*    @RequestMapping(value = {"login","tologin"},method = RequestMethod.GET)
    public String toLogin()
    {
        return page_login;
    }

    @RequestMapping(value= "login",method = RequestMethod.POST)
    public String login(@RequestParam(value = "userEmail",defaultValue = "") String username,
                        @RequestParam(value = "password",defaultValue = "") String password,
                        ModelMap map) throws Exception {

        if(Validation.isEmpty(username)|| Validation.isEmpty(password)) {
            map.addAttribute("errorMsg","Account and Password Can't not be blank");
            return page_login;
        }

        User user = service.selectByEmail(username);
        if(user ==null || !MD5.md5(password).equals(user.getPassword())) {
            addError(map,"Account or Password error");
            return page_login;
        }

        getSession().setAttribute(globalValName.user.toString(),user);
        logger.info("Login:" + user + " " + new Date());
        return "redirect:" + "/manage";
    }

    @RequestMapping("logout")
    public String logout()
    {
        getSession().setAttribute(globalValName.user.toString(),null);
        getSession().invalidate();

        return  "redirect:"+ to_login;
    }*/


    //Getters
    public static String getTo_login() {
        return to_login;
    }

    public static String getPage_login() {
        return page_login;
    }

    public static String getManage_home() {
        return manage_home;
    }
}
